//数据库-存入预定酒店信息
const cloud = require('wx-server-sdk')
const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
  var no = Math.floor(Math.random() * 999999);
  var date = new Date();
  var key = date.getTime() + "" + no;//暂时这样写。
  try {
    if(event.type2 == "set"){
      return await db.collection('userInfo').where({
        openid: event.openid
      })
      .update({
        data: {
          //viplevel:_.set(10),
          orderHotels: _.set(event.list)
        },
      })
    }
    return await db.collection('userInfo').where({
      openid: event.openid
    })
    .update({
      data: {
        //viplevel:_.set(10),
        orderHotels: _.push({
          key: key,
          id:event.id,
          name:event.name,
          icon:event.icon,
          num:event.num,
          price:event.price,
          totalPrice:event.totalPrice,
          time:event.time,
          rangeDate:event.rangeDate,
          regInfo:event.regInfo
        })
      },
    })
  } catch(e) {
    console.error("database orderHotels error!-->")
    console.error(e)
  }
}